package codejam11

object magicka {

	//===================================================================
	// receive invoke and 
	//return one character either invoked element or non-element as char
	// non_element_process("RE","FQT")
	def non_element_process( i1: Char, i2: Char, ne: String) :Char = 
	{
			// invoke => i1, i2
			//non-element
			//print("process(" + i1 +" , " + i2 +" , " + ne + " ) ")

			val nonel :Seq[Char] = ne
			if( (i1==nonel(0)) && (i2==nonel(1)) )
			{
				nonel(2)
			}
			else if( (i2==nonel(0)) && (i1==nonel(1)) )
			{
				nonel(2)
			}
			else
			{
				i1
			}		   
	}
	//===================================================================  

	def main(args: Array[String]) {
		//-----------------------------------------------------------
		// read data from the specific path
		val lines = io.Source.fromFile("D:/Program Files/Eclipse/ninja.hatoriz@hatoriz/Scala_test01/input/test_magicka.txt","utf-8").getLines
		// First line of input data
		val number_tc = lines.next.toInt
		1.to(number_tc).foreach { tc =>

		val l = lines.next.split(" ").map(_.mkString)

		//------- check C and create non element ----------------
		val c :Int = l(0).toInt
		// create array of string to contain non-element
		val nonar = new Array[String](c)
		if( c > 0 )
		{
			// iterate for every non-element following the numbers
			0.until(c).foreach
			{
				ci =>
				// keep non-element to array
				nonar(ci) = l(ci+1)
			}
		} 

		//------- check D and create opposed element ----------------
		val d :Int = l(c+1).toInt
		// create array of string to contain opposed element
		val opposedar = new Array[String](d)

		if( d > 0)
		{
			0.until(d).foreach
			{
				di =>
				opposedar(di) = l(c+di+2) //print(l(c+1+ di+1) + " ")
			}
		}

		//------- check N and invoke elements ----------------
		val n :Int = l(c+d+2).toInt
		val invokelist:String = l(c+d+3)
		val invokesq:Sequence[Char] = invokelist

		/// ********************** process here ********************
		print("Case# " + tc + ": [")
		//---------------------------------------------------------
		if( n == 1) // one invoke letter  // OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO N = 1, ignore C and D
		{
		 println("[" + invokesq(0) + "]") 
		}		
		else if( c == 0 && d == 0) // OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO N > 1, C and D is 0
		{
			0.until(n).foreach { ni =>
			print(invokesq(ni))
			if( n > 1 && ni < n-1)
			{
				print (", ")
			}	        
			}
			println("]")
		}
		else if (d == 0) // OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO N > 1, D is 0
		{
			var skip:Int = 0
		    //=======================================================
			0.until(n).foreach { ni => // number of element to be invoked
							
			  if( (ni+1) < n)
			  { 
			    val test: Char = non_element_process(invokesq(ni),invokesq(ni+1),"QRI" )
				// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			    if( invokesq(ni) == test && skip == 0)// (1) normal element ++
			    {
			      print(invokesq(ni) + ", ")
			    }
			    if(( invokesq(ni) != test) && skip == 0) // (2) form non-element already ++
				{
				  print(test)
				  skip = 1		  
				}
			    else if( skip == 1) // (3) form non-element already
				{
				  skip = 0
				}
			  }
			  else // (4) for the last invoke letter especially
			  {
			    if( skip == 0)
			    {
			      print(", " + invokesq(ni) )
			    }
			    
			  }
			
			}
			println("]")

		}
		else // OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO N > 1, C and D is not 0
		{
		  	// to be continue
		}

	}
 }
}